<?php
namespace App\Repository\Java;

use App\Repository\Contracts\AdminJavaInterface;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-02-06 13:33
 *
 * @Description:
 */
class AdminRepository extends Repository implements AdminJavaInterface
{
    protected function setPrefix()
    {
        $this->prefix = '';
    }

    public function authLogin($username = null, $password = null)
    {
        if ($username && $password && env('JAVA_USERNAME') === null) {
            $result = $this->post('system/login', ['userName' => $username, 'password' => $password], true);
            if ($result['resultType'] === 'success') {
                $fp = fopen('../.env', 'a');
                fwrite($fp, "\r\nJAVA_USERNAME=". $username ."\r\nJAVA_PASSWORD=". $password);
                fclose($fp);
            }

            return $result;
        }

        return $this->post('system/login', ['userName' => env('JAVA_USERNAME'), 'password' => env('JAVA_PASSWORD')], true);
    }

    public function searchStore($keyword)
    {
        $form['stockPlaceName'] = $keyword;
        $result = $this->post('stock/queryStockPlaceByConditionPaging', $form);

        return $result;
    }

    public function matchingStore(array $data)
    {
        $form['stockPlaceName'] = $data['storeName'];
        $form['empName'] = $data['shopkeeper'];
        $form['phoneNumber'] = $data['phone'];
        $result = $this->post('stock/preStockPlaceView', $form);

        return $result;
    }

    public function syncCreated($storeId, array $data)
    {
        $data['storeId'] = $storeId;
        $result = $this->post(__FUNCTION__, $data);

        return $result;
    }

    public function syncUpdated($id, array $data)
    {
        $data['id'] = $id;
        $result = $this->post(__FUNCTION__, $data);

        return $result;
    }
}